<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/8/2
 * Time: 10:37
 */

namespace app\everyday\controller;

use think\Controller;

class Tips extends Controller
{
    // 行运数据
    public $transitsData = [];
    // 天象数据
    public $nowData = [];

    public function init()
    {
        // TODO 第八阶段：每日运势小天象词条1(1)
//        print_r($this->transitsData);

        $all_check_phase = $this->getAllCheckPhase();
        $result = [];
        $house_tips = [
            [1,2,3,4,5,6,7,8,9,10,11,12],
            [13,14,15,16,17,18,19,20,21,22,23,24],
            [25,26,27,28,29,30,31,32,27,33,34,35],
            [36,37,38,39,40,41,42,43,38,44,45,46],
            [47,48,49,50,51,52,53,54,55,56,57,58],
            [59,60,61,62,63,64,65,66,67,68,69,70],
            [71,72,73,74,75,76,77,78,79,80,81,82],
            [83,84,85,86,87,88,89,90,91,92,93,94],
        ];

        foreach ($all_check_phase as $k1 => $d1)
        {
            foreach ($d1 as $k2 => $d2)
            {
                $house = $this->getHouseIdByTransitsPhase($d2);

                if(!empty($house))
                {
                    foreach ($house as $h)
                    {
                        if(!isset($result[$house_tips[$k1][$h['house']-1]]))
                        {
                            $result[$house_tips[$k1][$h['house']-1]] = ['num'=>1,'status'=>$h['status']];
                        }
                        else
                        {
                            $result[$house_tips[$k1][$h['house']-1]]['num']++;

                            if($h['status'] == 1) $result[$house_tips[$k1][$h['house']-1]]['status'] = 1;
                        }
                    }
                }
            }
        }

        // 1、重复两次以上的；2、有行运月亮冲合相位一方的；3、其它
        $result = resort($result,'num');

        $data = [];

        foreach ($result as $k => $res)
        {
            $data[] = $this->getTips($k);
        }

        return $data;
    }

    protected function getTips1()
    {
        $result = [];

        $tr_planet = $this->transitsData[1]['planets'];

        //tr水星刑冲冥王	    容易产生负面的想法，猜忌，纠结，自寻烦恼，也要小心与人冲突。
        if(checkPhase([2],[9],[90],$tr_planet))
        {
            $result[] = '容易产生负面的想法，猜忌，纠结，自寻烦恼，也要小心与人冲突。';
        }
        //tr水星合拱六分冥王	深入思考一些问题，尤其是与秘密、隐私有关的事情。或许会有坏消息，不开心的交流。
        if(checkPhase([2],[9],[0,60,120],$tr_planet))
        {
            $result[] = '深入思考一些问题，尤其是与秘密、隐私有关的事情。或许会有坏消息，不开心的交流。';
        }
        //tr水星刑冲海王	    迷糊，精神状态不佳，有胡思乱想的倾向，学习或考试不在状态。
        if(checkPhase([2],[8],[90,180],$tr_planet))
        {
            $result[] = '迷糊，精神状态不佳，有胡思乱想的倾向，学习或考试不在状态。';
        }
        //水星合拱六分海王	    想法多而丰富，忽悠人的功力得到提升，但要避免想得太多。
        if(checkPhase([2],[8],[0,60,120],$tr_planet))
        {
            $result[] = '想法多而丰富，忽悠人的功力得到提升，但要避免想得太多。';
        }
        //水星刑冲天王    	产生新的想法，有不期而至的消息，或突然的外出，小心出行中的小意外。
        if(checkPhase([2],[7],[90,180],$tr_planet))
        {
            $result[] = '产生新的想法，有不期而至的消息，或突然的外出，小心出行中的小意外。';
        }
        //水星刑冲土星	    沟通不畅，遭遇堵车，或有需要冥思苦想、耗费脑细胞的事情。
        if(checkPhase([2],[6],[90,180],$tr_planet))
        {
            $result[] = '沟通不畅，遭遇堵车，或有需要冥思苦想、耗费脑细胞的事情。';
        }
        //水星刑冲火星    	可能有带有火药味的交流，或是匆忙出行，出行运不佳时要小心意外。
        if(checkPhase([2],[4],[90,180],$tr_planet))
        {
            $result[] = '可能有带有火药味的交流，或是匆忙出行，出行运不佳时要小心意外。';
        }

        return $result;
    }

    /**
     * 获取所有需要检查的相位星体
     * @return array
     */
    private function getAllCheckPhase()
    {
        return [
            //tr金星冲合拱六分日月金水木（或合拱六分火星）/tr日金水冲合拱六分金星
            [
                ['planets1'=>[3],'planets2'=>[0,1,2,3,5],'phase_e'=>[0,60,120,180]],
                ['planets1'=>[3],'planets2'=>[4],'phase_e'=>[0,120,60]],
                ['planets1'=>[0,2,3],'planets2'=>[3],'phase_e'=>[0,60,120,180]],
            ],
            //tr水星合拱六分日月金水木/tr日金水合拱六分水星
            [
                ['planets1'=>[2],'planets2'=>[0,1,2,3,5],'phase_e'=>[0,60,120]],
                ['planets1'=>[0,2,3],'planets2'=>[2],'phase_e'=>[0,60,120]],
            ],
            //tr水星刑冲日月金水火土/tr日金刑冲水星或tr火合刑冲水星
            [
                ['planets1'=>[2],'planets2'=>[0,1,2,3,4,6],'phase_e'=>[90,180]],
                ['planets1'=>[0,3],'planets2'=>[2],'phase_e'=>[90,180]],
                ['planets1'=>[4],'planets2'=>[2],'phase_e'=>[0,90,180]],
            ],
            //tr火星合刑冲日月金水火木/tr日金水合刑冲火星
            [
                ['planets1'=>[4],'planets2'=>[0,1,2,3,4,5],'phase_e'=>[0,90,180]],
                ['planets1'=>[0,2,3],'planets2'=>[4],'phase_e'=>[0,90,180]],
            ],
            //"tr火星拱六分日月金水火木/tr日金水拱六分火星"
            [
                ['planets1'=>[4],'planets2'=>[0,1,2,3,4,5],'phase_e'=>[60,120]],
                ['planets1'=>[0,2,3],'planets2'=>[4],'phase_e'=>[60,120]],
            ],
            //"tr木星冲合拱六分日月且木星缓滞（近期）/tr太阳冲合拱六分木星"
            [
                ['planets1'=>[5],'planets2'=>[0,1],'phase_e'=>[0,60,120,180],'p1_slow'=>true],
                ['planets1'=>[0],'planets2'=>[5],'phase_e'=>[0,60,120,180]],
            ],
            //"tr木星冲合拱六分金星且木星缓滞（近期）/tr金星冲合拱六分木星"
            [
                ['planets1'=>[5],'planets2'=>[3],'phase_e'=>[0,60,120,180],'p1_slow'=>true],
                ['planets1'=>[3],'planets2'=>[5],'phase_e'=>[0,60,120,180]],
            ],
            //"tr木星冲合拱六分水星且木星缓滞（近期）/tr水星冲合拱六分木星"
            [
                ['planets1'=>[5],'planets2'=>[2],'phase_e'=>[0,60,120,180],'p1_slow'=>true],
                ['planets1'=>[2],'planets2'=>[5],'phase_e'=>[0,60,120,180]],
            ],
        ];
    }

    protected function getTips(int $key)
    {
        $tips=config('fortune.astro_tips');
        return $tips[$key];
    }

    /**
     * 检查相位是否符合条件
     * @param array $array
     * @return array 返回成相星体涉及的宫位
     */
    protected function getHouseIdByTransitsPhase(array $array)
    {
        $result = [];
        if(!isset($array['planets1']) || !isset($array['planets2']) || !isset($array['phase_e'])
            || empty($array['planets1']) || empty($array['planets2']) || empty($array['phase_e']))
        {
            return $result;
        }
        $p1_slow = false;
        if(isset($array['p1_slow']))
        {
            $p1_slow = $array['p1_slow'];
        }

        $tr_planet1 = $this->transitsData[1]['planets'];// 行运盘外盘数据
        $tr_planet2 = $this->transitsData[0]['planets'];// 行运盘本命数据
        $tx_planet = $this->nowData['planets'];// 天象数据

        foreach ($array['planets1'] as $p1)
        {
            if(!empty($tr_planet1[$p1]['phase']))
            {
                foreach ($tr_planet1[$p1]['phase'] as $phase)
                {
                    if(in_array($phase['id'],$array['planets2']) && in_array($phase['phase_e'],$array['phase_e']))
                    {

                        // 是否被行运月亮冲合
                        $status1 = 0;
                        $status2 = 0;
                        if(isset($tx_planet[$p1]['phase'][$p1.'-1'])
                            && in_array($tx_planet[$p1]['phase'][$p1.'-1']['phase_e'],[0,180]))
                        {
                            $status1 = 1;
                        }
                        if(isset($tr_planet1[1]['phase']['1-'.$phase['id']])
                            && in_array($tr_planet1[1]['phase']['1-'.$phase['id']]['phase_e'],[0,180]))
                        {
                            $status2 = 1;
                        }

//                        echo "$p1 --- {$phase['id']} ----  {$phase['phase_e']}\n";
                        $normal_speeds = config('astrolog.speed');

                        $normal_p1_speed = isset($normal_speeds[$p1]) ? $normal_speeds[$p1] : 0;

                        if(($p1_slow === true && $tr_planet1[$p1]['speed'] <= $normal_p1_speed) || $p1_slow === false)
                        {
                            $result[] = ['house'=>$tr_planet1[$p1]['in_house_id'],'status'=>$status1];

                            $result[] = ['house'=>$tr_planet2[$phase['id']]['in_house_id'],'status'=>$status2];
                        }
                    }
                }
            }
        }

        return $result;
    }

}